package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.common;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireballEntity;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;

import java.util.ArrayList;
import java.util.List;

public class TraceballSkill extends BaseSkill {

    List<SaveFireballEntity> fireballEntities = new ArrayList<>();

    public TraceballSkill(int level) {
        super(level);
    }

    public TraceballSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public TraceballSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(97);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("追踪技能，可以发射追踪的火球。随等级提升伤害。冷却时间 %d tick", skillConfig.getCooldown());
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown());
            LivingEntity target = mobEntity.getTarget();
            double f = target.getX() - (mobEntity.getX());
            double g = target.getBodyY(0.5) - (0.5 + mobEntity.getBodyY(1.5));
            double h = target.getZ() - (mobEntity.getZ());
            SaveFireballEntity fireballEntity = new SaveFireballEntity(mobEntity.world, mobEntity, f, g, h, target);
            fireballEntity.explosionPower = getSkillLevel();
            fireballEntity.hitDamage = getSkillLevel() * 3;
            fireballEntity.setPosition(mobEntity.getX(), mobEntity.getBodyY(1.5) + 0.5, fireballEntity.getZ());
            mobEntity.world.spawnEntity(fireballEntity);
        }
        return true;
    }
}
